package leetcode;

import java.util.*;

public class LeetCode17 {

    public static void main(String[] args) {
        LeetCode17 leetCode17 = new LeetCode17();
        leetCode17.letterCombinations1("234");
    }

    public List<String> letterCombinations1(String digits) {
        Map<Character, String> phone = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        List<String> result = new ArrayList<>();
        if (digits.length() == 0)
            return result;
        List<String> tmp = new ArrayList<>();
        for (char c : phone.get(digits.charAt(0)).toCharArray()) {
            result.add(c + "");
        }
        System.out.println(result);
        for (int i = 1; i < digits.length(); i++) {
            tmp.clear();
            String s = phone.get(digits.charAt(i));
            for (Character c : s.toCharArray()) {
                for (String str : result) {
                    tmp.add(str + c);
                }
            }
            result.clear();
            result.addAll(tmp);
        }
        return result;
    }

    //------------------------------------------------------------------
    Map<String, String> phone = new HashMap<String, String>() {{
        put("2", "abc");
        put("3", "def");
        put("4", "ghi");
        put("5", "jkl");
        put("6", "mno");
        put("7", "pqrs");
        put("8", "tuv");
        put("9", "wxyz");
    }};

    List<String> result = new ArrayList<>();

    String string = "";

    public List<String> letterCombinations(String digits) {
        string = digits;
        calculation("", 0);
        return result;
    }

    private void calculation(String tmpResult, int index) {
        if (index == string.length()) {
            result.add(tmpResult);
            return;
        }
        String s = phone.get(string.substring(index, index + 1));
        for (int i = 0; i < s.length(); i++) {
            calculation(tmpResult + s.substring(i, i + 1), index++);
        }
    }
}
